package com.steven.im.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.steven.im.R;
import com.steven.im.entity.RechargeMoneyEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Steven on 2019/2/13.
 */
public class RechargeMoneyAdapter extends BaseQuickAdapter<RechargeMoneyEntity, BaseViewHolder> {
    public String Money;
    private List<RechargeMoneyEntity> data;
    private List<View> list;
    private String money;
    private TextView tv_get;

    public RechargeMoneyAdapter(Context mContext, @Nullable List<RechargeMoneyEntity> data, String money, TextView tv_get) {
        super(R.layout.recharge_money_item, data);
        this.mContext = mContext;
        this.data = data;
        this.money = money;
        this.tv_get = tv_get;
        list = new ArrayList<>();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void convert(final BaseViewHolder helper, final RechargeMoneyEntity item) {
        if (helper.getPosition() == data.size() - 1) {
            helper.getView(R.id.tv_money).setVisibility(View.GONE);
            helper.getView(R.id.edit_money).setVisibility(View.VISIBLE);
            ((EditText) helper.getView(R.id.edit_money)).setHint(item.getMoney());
            list.add(helper.getView(R.id.edit_money));
        } else {
            ((TextView) helper.getView(R.id.tv_money)).setText(item.getMoney());
            list.add(helper.getView(R.id.tv_money));
        }
        if (money.equals(item.getMoney())) {
            helper.getView(R.id.tv_money).setBackground(mContext.getResources().getDrawable(R.drawable.recharge_money_focous_shape));
        }
        ((EditText) helper.getView(R.id.edit_money)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable editable) {
                String str = ((EditText) helper.getView(R.id.edit_money)).getText().toString();
                if (str.length() > 0) {
                    Money = str;
                    tv_get.setText(str);
                } else {
                    Money = "";
                    tv_get.setText(0 );
                }
            }
        });
        helper.getView(R.id.edit_money).setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View view) {
                String str = ((EditText) helper.getView(R.id.edit_money)).getText().toString();
                if ("".equals(str)) {
                    tv_get.setText("0");
                }else {
                    tv_get.setText(str);
                }
                for (View aList : list) {
                    if (aList.equals(view)) {
                        aList.setBackground(mContext.getResources().getDrawable(R.drawable.recharge_money_focous_shape));
                    } else {
                        aList.setBackground(mContext.getResources().getDrawable(R.drawable.recharge_money_shape_normol));
                    }
                }
            }
        });
        helper.getView(R.id.tv_money).setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View view) {
                Money = item.getMoney();
                tv_get.setText(Money );
                for (View aList : list) {
                    if (aList.equals(view)) {
                        aList.setBackground(mContext.getResources().getDrawable(R.drawable.recharge_money_focous_shape));
                    } else {
                        aList.setBackground(mContext.getResources().getDrawable(R.drawable.recharge_money_shape_normol));
                    }
                }
            }
        });
    }
}
